"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path = require("path");
var fs = require("fs");
var ts = require("typescript");
function findEnumReferences(filePath, enumName) {
    var program = ts.createProgram([filePath], {});
    var checker = program.getTypeChecker();
    var sourceFile = program.getSourceFile(filePath);
    if (!sourceFile) {
        console.error("File not found");
        return;
    }
    var enumReferencesCount = {};
    function visit(node) {
        if (ts.isPropertyAccessExpression(node) &&
            ts.isIdentifier(node.expression)) {
            var symbol = checker.getSymbolAtLocation(node.expression);
            if (symbol && symbol.getName() === enumName) {
                var memberName = node.name.text;
                enumReferencesCount[memberName] =
                    (enumReferencesCount[memberName] || 0) + 1;
            }
        }
        ts.forEachChild(node, visit);
    }
    ts.forEachChild(sourceFile, visit);
    console.log(filePath, enumReferencesCount);
    return enumReferencesCount;
}
/**
 * 在 vscode 中打开文件 src\components\authorize\Permissions.ts
 *
 * 右键 EnumPermissions 找到所有引用
 *
 * 在 references 中右键 copy all
 *
 * 将内容保存到 references.txt 文件中
 */
function getAllPath() {
    var projectPath = "D:\\ThePiqueLab\\archus-crm-ui";
    // 读取文件内容
    var data = fs.readFileSync("references.txt", "utf8");
    // 使用正则表达式匹配所有以`src/`开头的文件路径
    var regex = /^src\/.*$/gm;
    var matches;
    var fullPaths = [];
    while ((matches = regex.exec(data)) !== null) {
        // 这是必须的，以避免死循环
        if (matches.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        // matches[0] 包含了匹配到的完整文本，即文件路径
        if (matches[0]) {
            // 使用path.join来确保路径在不同操作系统上的正确性
            // path.normalize将会处理任何跨平台的路径问题
            var fullPath = path.normalize(path.join(projectPath, matches[0]));
            fullPaths.push(fullPath);
        }
    }
    // 去重并输出所有的文件路径
    // const uniquePaths = [...new Set(fullPaths)];
    // console.log(uniquePaths.join("\n"));
    return fullPaths;
}
var allPaths = getAllPath();
console.log('path count:', allPaths.length);
var result = {};
allPaths.forEach(function (p) {
    // 示例：查找并统计枚举键的引用次数
    var filePath = path.resolve(p);
    var enumName = "EnumPermissions";
    var referencesCount = findEnumReferences(filePath, enumName);
    // console.log(referencesCount);
    Object.entries(referencesCount).forEach(function (_a) {
        var key = _a[0], value = _a[1];
        result[key] = (result[key] || 0) + value;
    });
});
console.log(result);
